﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using XueMeng.BLL;
using XueMeng.Model;
using Maticsoft.Common;
using System.Text.RegularExpressions;

namespace UI
{
    public partial class EditTask : Form
    {

        /**
         * 
         * 
          **/
        private readonly TaskManager tm = new TaskManager();
        private readonly ProblemManager pm = new ProblemManager();


        public EditTask()
        {
            InitializeComponent();
        }

        private void EditTask_Load(object sender, EventArgs e)
        {
            LoadData();
            LoadEvent();
        }

        private void LoadEvent()
        {
            this.textBox1.KeyUp += new KeyEventHandler(textBox1_KeyUp);
            this.button1.Click += new EventHandler(button1_Click);
            this.button2.Click += new EventHandler(button2_Click);
            this.FormClosed += new FormClosedEventHandler(EditTask_FormClosed);
        }

        void EditTask_FormClosed(object sender, FormClosedEventArgs e)
        {
            Help.TemporarySet.Problems = null;
            Help.TemporarySet.Student = null;
        }

        void button2_Click(object sender, EventArgs e)
        {
            if (Help.TemporarySet.Student==null)
            {
                System.Windows.Forms.MessageBox.Show("请选择选择学生");
                return;
            }
            if ((!Regex.IsMatch(this.domainUpDown1.Text,@"^[1-9]\d*$"))||(!Regex.IsMatch(this.domainUpDown2.Text,@"^[1-9]\d*$")))
            {
                System.Windows.Forms.MessageBox.Show("请输入正确的分数");
                return;
            }
            int no1 = Convert.ToInt32(this.domainUpDown1.Text);
            int no2 = Convert.ToInt32(this.domainUpDown2.Text);
            Task t = new Task()
            {
                T_Name = Convert.ToString(this.textBox2.Text.Trim()),
                Fraction_Sta = no1<=no2?no1:no2,
                Fraction_Sto = no1>no2?no1:no2,
                S_Id = Help.TemporarySet.Student.S_Id,
                T_Date = DateTime.Now,
                X_Id = Help.UserInfo.User.E_Id,
                Enable = 0
            };
            SaveTask(t);
            CreateProblem();
        }

        private void SaveTask(Task t)
        {
            if (tm.Add(t)>0)
            {
                SaveProblem(tm.GetModel(t).T_Id);
                System.Windows.Forms.MessageBox.Show("保存成功");
                this.Close();
            }
        }

        private void SaveProblem(int T_Id)
        {
            List<Problem> list = Help.TemporarySet.Problems;
            foreach (var item in list)
            {
                item.Enable = 0;
                item.T_Id = T_Id;
                pm.Add(item);
            }
        }

        private List<Problem> CreateProblem()
        {
            List<Problem> list = new List<Problem>();

            return list;
        }

        void button1_Click(object sender, EventArgs e)
        {
            EditProblem ep = new EditProblem();
            ep.ShowDialog();
            this.listView1.Clear();
            this.listView1.Columns.Add("问题",320);
            foreach (var item in Help.TemporarySet.Problems)
            {
                ListViewItem litem = new ListViewItem();
                litem.Tag = item;
                litem.Text = item.P_Context;
                this.listView1.Items.Add(litem);
            }
        }

        void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            OpenForm();
        }

        private void OpenForm()
        {
            StudentMain sm = new StudentMain();
            sm.ShowDialog();
            if (Help.TemporarySet.Student == null)
            {
                this.textBox1.Text = "打开窗体后请按F3取回数据";
                Help.TemporarySet.Student = null;
                return;
            }
            this.textBox1.Text = Help.TemporarySet.Student.S_Name;
        }

        private void LoadData()
        {
            //throw new NotImplementedException();
        }

    }
}
